//
//  StaffDetailViewController.m
//  Marketing
//
//  Created by wmm on 16/3/11.
//  Copyright © 2016年 Hanen 3G 01. All rights reserved.
//

#import "StaffDetailViewController.h"
#import "QRCodeGenerator.h"

@interface StaffDetailViewController ()<UITableViewDelegate,UITableViewDataSource>{
    
    NSArray     *_titleArray;
    UIImageView *_bgImage1;
    UILabel     *qrNameLabel;
     UIButton    *QRbutton;
}

@property(nonatomic,strong)NSDictionary   *dictionary;
@property(nonatomic,strong)UIView         *qrView;
@property(nonatomic,strong)UIImageView    *qrImageView;
@property(nonatomic,strong)UIImageView    *bgImage;

@property(nonatomic,strong)UITableView *tableView;

@end

@implementation StaffDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.titleView = [ViewTool getNavigitionTitleLabel:@"个人信息"];
    self.navigationItem.leftBarButtonItem = [ViewTool getBarButtonItemWithTarget:self WithAction:@selector(popViewController)];
    UIButton *qrBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 20, 20)];
    [qrBtn setImage:[UIImage imageNamed:@"HY_nav_icon_QR"] forState:UIControlStateNormal];
    [qrBtn addTarget:self action:@selector(getQRcode:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:qrBtn];
    
    //    _dictionary = [[NSDictionary alloc]init];
    
//        [self initData];

    
    [self createUI];
    
    [self createQRview];
    
 
}

//- (void)initData{
//    
//        NSDictionary * dic =@{@"token":TOKEN,@"uid":@(UID),@"userid":@(self.user.uid)};
//        [DataTool sendGetWithUrl:GET_USER_BY_USERID_URL parameters:dic success:^(id data) {
//            NSDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
//    
//    
//    
//            NSLog(@"%@+++++--wodezhuye",jsonDic);
//    
//            _dictionary = jsonDic ;
//            NSLog(@"%@  dic+++++",self.dictionary);
//    
////            _nameLabel.text = _dictionary[@"name"];
//    
////            NSString  *imageStr =[NSString stringWithFormat:@"%@%@",IMAGEURL,_dictionary[@"img"]];
////            [_headerBtn sd_setImageWithURL:[NSURL URLWithString:imageStr] forState:UIControlStateNormal];
//    
////            NSData *data1 = [NSData dataWithContentsOfURL:[NSURL URLWithString:imageStr]];
////            _bgImage1.image = [UIImage imageWithData:data1];
//    
//            _phoneLabel.text = [NSString stringWithFormat:@"手机号:%@",_dictionary[@"username"]];
//    
//    
//            NSString  *qrImageStr =[NSString stringWithFormat:@"%@%@",IMAGEURL,_dictionary[@"logo"]];
//            _qrImageView.image = [QRCodeGenerator qrImageForString:qrImageStr imageSize:300];
//    
//            qrNameLabel.text = _dictionary[@"name"];
//    
//        } fail:^(NSError *error) {
//            NSLog(@"%@",error);
//        }];
//}

- (void)createUI{
    _titleArray = [[NSArray alloc]initWithObjects:@"所属部门",@"职位", nil];
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, KSCreenW, KSCreenH - 49) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.scrollEnabled =NO;
    _tableView.backgroundColor = graySectionColor;
    _tableView.rowHeight = [UIView getHeight:50.0f];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:_tableView];
    
    CGFloat bgImageH     = [UIView getHeight:210];//背景图片的高度
    CGFloat headerImageW  = [UIView getWidth:90];//头像尺寸
//    CGFloat qrSpace      = 15;
//    CGFloat qrW          = [UIView getWidth:40];
    
    NSString *urlStr = [NSString stringWithFormat:@"%@%@",LoadImageUrl,_user.logo];
    NSData * data = [NSData dataWithContentsOfURL:[NSURL URLWithString:urlStr]];
    
    
    UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KSCreenW, bgImageH)];
    headerView.backgroundColor = NavBackColor;
    
    //大背景图片
    _bgImage1 = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, KSCreenW, bgImageH)];
    _bgImage1.backgroundColor = NavBackColor;
//    _bgImage1.image = [UIImage imageWithData:data];
    [headerView addSubview:_bgImage1];
    //毛玻璃效果
//    UIBlurEffect *blur = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
//    UIVisualEffectView *effectview = [[UIVisualEffectView alloc] initWithEffect:blur];
//    
//    effectview.frame = CGRectMake(0, 0, KSCreenW, bgImageH);
//    
//    [_bgImage1 addSubview:effectview];
    
//    UILabel *nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, [UIView getHeight:30], KSCreenW, [UIView getHeight:40])];
//    nameLabel.text = _user.name;
//    nameLabel.textColor = [UIColor whiteColor];
//    nameLabel.textAlignment = NSTextAlignmentCenter;
////    [headerView addSubview:nameLabel];
    
    UIButton *headerBtn = [[UIButton alloc]initWithFrame:CGRectMake((KSCreenW-headerImageW)/2, 0, headerImageW, headerImageW)];
    headerBtn.center = CGPointMake(KSCreenW / 2.0f, headerView.height / 2.0f - 20);
    headerBtn.backgroundColor = [UIColor cyanColor];
    headerBtn.layer.cornerRadius = headerImageW/2;
    [headerBtn.layer setBorderWidth:5.0];
    [headerBtn.layer setBorderColor:UIColorFromRGB(0x8095c4).CGColor];//边框颜色

    headerBtn.layer.masksToBounds = YES;
    
    [headerBtn setBackgroundImage:[UIImage imageWithData:data] forState:UIControlStateNormal];
    
    [headerBtn addTarget:self action:@selector(clickToMyInformation:) forControlEvents:UIControlEventTouchUpInside];
    [headerView addSubview:headerBtn];
    
    
    UILabel *nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, headerBtn.maxY + 20, KSCreenW, 20.0f)];
    nameLabel.text = _user.name;
    nameLabel.textColor = UIColorFromRGB(0xc4cbdf);
    nameLabel.textAlignment = NSTextAlignmentCenter;
        [headerView addSubview:nameLabel];
    nameLabel.font = [UIFont systemFontOfSize:15.0f];
    
//    UIImageView *photoImageView = [[UIImageView alloc]initWithFrame:CGRectMake(headerBtn.maxX - [UIView getWidth:20], headerBtn.maxY -[UIView getHeight:20] , [UIView getWidth:20], [UIView getHeight:20])];
//    photoImageView.image =[UIImage imageNamed:@"相机"];
//    [headerView addSubview:photoImageView];
    
    
//    UIImageView *phoneImageView = [[UIImageView alloc]initWithFrame:CGRectMake(KSCreenW/3, nameLabel.maxY, [UIView getHeight:20], [UIView getHeight:20])];
//    phoneImageView.image =[UIImage imageNamed:@"电话2.png"];
//    [headerView addSubview:phoneImageView];
    
    UILabel *phoneLabel = [[UILabel alloc]initWithFrame:CGRectMake(KSCreenW/2 - 250 / 2.0f, nameLabel.maxY, 250, [UIView getHeight:20])];
    phoneLabel.text = [NSString stringWithFormat:@"手机号:%@",_user.username];
    phoneLabel.textColor=UIColorFromRGB(0xc4cbdf);
    phoneLabel.font = [UIFont systemFontOfSize:15.0f];
     phoneLabel.textAlignment = NSTextAlignmentCenter;
    [headerView addSubview:phoneLabel];
    
    _tableView.tableHeaderView = headerView;
}
- (void)createQRview{
    
    UIWindow *window = [UIApplication sharedApplication].delegate.window;
    
    _qrView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KSCreenW, KSCreenH)];
    _qrView.backgroundColor = [UIColor blackColor];
    _qrView.hidden = YES;
    [window addSubview:_qrView];
    
    _bgImage = [[UIImageView alloc]initWithFrame:CGRectMake([UIView getWidth:60], [UIView getHeight:120], KSCreenW - 2*[UIView getWidth:60], KSCreenW - 2*[UIView getWidth:60])];
    //    _bgImage.image = [UIImage imageNamed:@"二维码bg"];
    _bgImage.backgroundColor = [UIColor whiteColor];
    _bgImage.hidden = YES;
    _bgImage.layer.cornerRadius = 5;
    _bgImage.layer.masksToBounds = YES;
    [window addSubview:_bgImage];
    
    QRbutton = [[UIButton alloc]initWithFrame:CGRectMake((KSCreenW -40) / 2, _bgImage.maxY + 50, 40, 40)];
    [QRbutton setImage:[UIImage imageNamed:@"HY_btn_close"] forState:UIControlStateNormal];
    [QRbutton addTarget:self action:@selector(hiddenQRView) forControlEvents:UIControlEventTouchUpInside];
    QRbutton.hidden = YES;
    [window addSubview:QRbutton];
    
    qrNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, [UIView getHeight:20], KSCreenW - 2*[UIView getWidth:40], [UIView getHeight:30])];
    qrNameLabel.textColor = [UIColor whiteColor];
    qrNameLabel.textAlignment = NSTextAlignmentCenter;
    [_bgImage addSubview:qrNameLabel];
    
    _qrImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, _bgImage.width, _bgImage.height)];
    [_bgImage addSubview:_qrImageView];
    
    
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(toHiddenQRView:)];
    [_qrView addGestureRecognizer:tapGesture];
    
    
    NSString *str = [DataTool changeType:self.user.img];
    NSString  *qrImageStr =[NSString stringWithFormat:@"%@%@",LoadImageUrl,str];
    NSLog(@"%@",qrImageStr);
    _qrImageView.image = [QRCodeGenerator qrImageForString:qrImageStr imageSize:300];
    
}

//生成二维码
- (void)getQRcode:(UIButton *)btn{
    [UIView animateWithDuration:0.25 animations:^{
        _qrView.hidden = NO;
        _qrView.alpha = 0.7;
        _bgImage.hidden = NO;
        QRbutton.hidden = NO;
    }];
    
}
- (void)hiddenQRView{
    [UIView animateWithDuration:0.25 animations:^{
        _qrView.hidden = YES;
        _bgImage.hidden = YES;
        QRbutton.hidden = YES;
    }];
}
- (void)toHiddenQRView:(UITapGestureRecognizer *)tap{
    [UIView animateWithDuration:0.25 animations:^{
        _qrView.hidden = YES;
        _bgImage.hidden = YES;
        QRbutton.hidden = YES;
    }];
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = NO;
    self.tabBarController.hidesBottomBarWhenPushed = YES;
    //    _departmentTableView.clearsSelectionOnViewWillAppear = NO;
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    self.tabBarController.hidesBottomBarWhenPushed = NO;
}

- (void)popViewController
{
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [UIView animateWithDuration:0.25 animations:^{
        _qrView.hidden = YES;
        _bgImage.hidden = YES;
    }];
}
//进入我的资料
- (void)clickToMyInformation:(UIButton *)btn{
    //    InformationViewController *vc = [[InformationViewController alloc]init];
    //    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark
#pragma mark---------
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _titleArray.count;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *vie = [ViewTool getLineViewWith:CGRectMake(0, 0, KSCreenW, 17.0f) withBackgroudColor:graySectionColor];
    vie.layer.borderColor = grayLineColor.CGColor;
    vie.layer.borderWidth = 1;
//      UIView *l1 = [ViewTool getLineViewWith:CGRectMake(0, 0, KSCreenW, 1.0f) withBackgroudColor:grayLineColor];
//      UIView *l2 = [ViewTool getLineViewWith:CGRectMake(0, vie.height - 1, KSCreenW, 1.0f) withBackgroudColor:grayLineColor];
//    [vie addSubview:l1];
//    [vie addSubview:l2];
    return vie;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 17.0f;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 44;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.backgroundColor = [UIColor whiteColor];
    UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(20, 0, 80.0f, 44.0f)];
    title.text = _titleArray[indexPath.row];
    title.font = [UIFont systemFontOfSize:15];
    title.textColor = grayFontColor;
    [cell addSubview:title];
    
    UILabel *value = [[UILabel alloc] initWithFrame:CGRectMake(title.maxX + 10.0f, title.y, 250.0f, title.height)];
    value.font = [UIFont systemFontOfSize:16];
    value.textColor = blackFontColor;
//    value.backgroundColor = NavBackColor;
    
    if (indexPath.row == 0) {
        value.text = _user.departname;
    }else{
        value.text = _user.positionname;
    }
    [cell addSubview:value];
//    if(indexPath.row == _titleArray.count - 1){
    [cell addSubview:[ViewTool getLineViewWith:CGRectMake(title.x, title.maxY, KSCreenW- title.x, 1) withBackgroudColor:grayListColor]];
//    }
     if(indexPath.row == _titleArray.count - 1){
         [cell addSubview:[ViewTool getLineViewWith:CGRectMake(0, title.maxY, KSCreenW, 1) withBackgroudColor:grayListColor]];
     }
    return cell;
    
}

//- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//    switch (indexPath.row) {
//        case 0:
//        {BusinessAdressBookVC *vc = [[BusinessAdressBookVC alloc]init];
//            [self.navigationController pushViewController:vc animated:YES];}
//            break;
//        case 1:
//        { InviteViewController *vc = [[InviteViewController alloc]init];
//            [self.navigationController pushViewController:vc animated:YES];}
//            break;
//        case 2:
//        {SettingViewController *vc = [[SettingViewController alloc]init];
//            [self.navigationController pushViewController:vc animated:YES];}
//            break;
//        default:
//            break;
//    }
//}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
